package com.autumn.config.entities;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.Table;

import com.autumn.mybatis.mapper.annotation.TableOrderBy;
import com.autumn.util.StringUtils;

/**
 * 应用配置
 * 
 * @author 老码农
 *         <p>
 *         Description
 *         </p>
 * @date 2018-01-21 23:30:14
 */
@Entity
@Table(name = "app_info_profile")
@TableOrderBy(value = "appId ASC,sortId ASC")
public class ApplicationProfile extends AbstractConfigureModifiedGmt implements IAppConfigureDefaultEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -779613168030455296L;

	/**
	 * 连接引用前缀
	 */
	public final static String CONNECTION_REF_SUFFIX = "$ref ";

	/**
	 * properties 格式
	 */
	public final static String FORAMT_PROPERTIES = "properties";

	/**
	 * yml 格式
	 */
	public final static String FORAMT_YML = "yml";

	private Long appId;
	private String name;
	private String label;
	private String content;
	private String profileFormat;
	private Integer version;
	private boolean isDefault;

	/**
	 * 获取应用Id
	 * 
	 * @return
	 */
	public Long getAppId() {
		return appId;
	}

	/**
	 * 设置应用Id
	 * 
	 * @param appId
	 *            应用Id
	 */
	public void setAppId(Long appId) {
		this.appId = appId;
	}

	/**
	 * 获取配置名称
	 * 
	 * @return
	 */
	public String getName() {
		return name;
	}

	/**
	 * 设置配置名称
	 * 
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 获取标签(分支)
	 * 
	 * @return
	 */
	public String getLabel() {
		return label;
	}

	/**
	 * 设置标签(分支)
	 * 
	 * @param label
	 *            标签(分支)
	 */
	public void setLabel(String label) {
		this.label = label;
	}

	/**
	 * 获取配置内容
	 * 
	 * @return
	 */
	public String getContent() {
		return content;
	}

	/**
	 * 设置配置内容
	 * 
	 * @param content
	 *            配置内容
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * 配置格式
	 * 
	 * @return
	 *
	 */
	public String getProfileFormat() {
		return profileFormat;
	}

	/**
	 * 配置格式
	 * 
	 * @param profileFormat
	 *
	 */
	public void setProfileFormat(String profileFormat) {
		this.profileFormat = profileFormat;
	}

	/**
	 * 获取版本
	 * 
	 * @return
	 */
	public Integer getVersion() {
		return version;
	}

	/**
	 * 设置版本
	 * 
	 * @param version
	 *            版本
	 */
	public void setVersion(Integer version) {
		this.version = version;
	}

	/**
	 * 获取是否默认
	 * 
	 * @return
	 */
	@Override
	public boolean getIsDefault() {
		return isDefault;
	}

	/**
	 * 设置是否默认
	 * 
	 * @param isDefault
	 *            是否默认
	 */
	@Override
	public void setIsDefault(boolean isDefault) {
		this.isDefault = isDefault;
	}

	/**
	 * 读取引用
	 * 
	 * @param propertiesMap
	 * @return key = 连接名称,value 为属性集合
	 */
	public static Map<String, List<String>> readRefSet(Map<String, Object> propertiesMap) {
		Map<String, List<String>> refMap = new LinkedHashMap<>(propertiesMap.size());
		for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
			Object value = entry.getValue();
			if (value != null) {
				String propertiesValue = value.toString().toLowerCase();
				if (propertiesValue.length() > CONNECTION_REF_SUFFIX.length()
						&& propertiesValue.startsWith(CONNECTION_REF_SUFFIX)) {
					String connectionName = propertiesValue.substring(CONNECTION_REF_SUFFIX.length());
					if (!StringUtils.isNullOrBlank(connectionName)) {
						String key = connectionName.toLowerCase().trim();
						List<String> propertiesList = refMap.get(key);
						if (propertiesList == null) {
							propertiesList = new ArrayList<>();
							refMap.put(key, propertiesList);
						}
						propertiesList.add(entry.getKey());
					}
				}
			}
		}
		return refMap;
	}

}
